package com.zx.AnimationThread;

import java.util.ArrayList;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PetalView extends SurfaceView implements SurfaceHolder.Callback{
	
	public static final int DIE_OUT_LINE = 800;
	PetalSet ps;
	PetalThread pt;
	DrawThread dt;
	
	public PetalView(Context context) {
		super(context);
		setBackgroundResource(R.drawable.icon);
		getHolder().addCallback(this);
		ps = new PetalSet();
		pt = new PetalThread(this,getResources());
		dt = new DrawThread(this, getHolder());
	}


	
	public void doDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		ArrayList<Petal> petalSet = ps.petalSet;
		Paint paint = new Paint();
		
		for(int i = 0; i < petalSet.size(); i++) {
			Petal petal = petalSet.get(i);
			int tempX = petal.x;
			int tempY = petal.y;
			
			canvas.drawBitmap(petal.Image, tempX, tempY, paint);
		}
	}
	
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		if(!pt.isAlive()) {
			pt.start();
		}
		if(!dt.isAlive()) {
			dt.start();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		dt.flag = false;
		dt = null;
		pt.flag = false;
		pt = null;
	}

}
